#include <iostream>
#include <iomanip>
using namespace std;

class Date
{
public:
    /* 默认构造函数，以fullyear的形式给出年月日，默认值为1990年1月1日，同时设置日期分隔符为“-” */
    Date(int year = 1990, int month = 1, int day = 1)
    {
        this->year = year;
        this->month = month;
        this->day = day;
        this->separator = '-';
    }

    /* get、set方法 */
    // 设置日期，如果有非法的月或日，将其置为1
    void setDate(int year, int month, int day);
    void setYear(int year)
    {
        this->year = year;
    }
    int getYear()
    {
        return this->year;
    }
    void setMonth(int month)
    {
        this->month = month;
    }
    int getMonth()
    {
        return this->month;
    }
    void setDay(int day)
    {
        this->day = day;
    }
    int getDay()
    {
        return this->day;
    }
    void setSeparator(char separator)
    {
        this->separator = separator;
    }

    /* 输出函数，请使用setfill(‘0’)和setw(2)，需要包含<iomanip>头文件 */
    void printFullYear() // 以YYYY-MM-DD的形式打印，2011-01-08
    {
        cout << this->year << this->separator << setfill('0') << setw(2) << this->month << this->separator << setfill('0') << setw(2) << this->day << endl;
    }
    void printStandardYear() // 以YY-MM-DD的形式打印，比如11-01-08
    {
        int temp = 0;
        int year = this->year;
        temp = year % 10;
        year /= 10;
        temp += year % 10 * 10;
        cout << setfill('0') << setw(2) << temp << this->separator << setfill('0') << setw(2) << this->month << this->separator << setfill('0') << setw(2) << this->day << endl;
    }
    /*  计算函数  */
    // 计算当前日期与参数日期之间相差几个整年，仅考虑参数日期比当前日期晚的情况
    int fullYearsTo(int year, int month, int day);
    /* 计算当前日期与参数日期之间相差多少天(考虑闰年)，如果参数日期在当前日期之前，返回负数。 */
    int daysTo(int year, int month, int day);

private:
    int year;
    int month;
    int day;
    char separator; // 日期分隔符
};

int main()
{
    Date birthDate(1969, 8, 11);
    birthDate.printFullYear();     // 打印：1969-08-11
    birthDate.printStandardYear(); // 打印：69-08-11
    birthDate.setSeparator('/');
    birthDate.printFullYear();                          // 打印：1969/08/11
    cout << birthDate.fullYearsTo(2010, 4, 15) << endl; // 打印：40，满四十岁
    cout << birthDate.daysTo(2010, 4, 15) << endl;      // 打印14857，活了14857天了
    // 打印-7254，共和国比我早诞生了7254天
    cout << birthDate.daysTo(1949, 10, 1) << endl;
    return 0;
}
//判断是否为闰年
bool isLeap(int year)
{
    bool flag;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        flag = true;
    }
    else
        flag = false;
    return flag;
}
void Date::setDate(int year, int month, int day)
{
    this->year = year;
    if (month > 12 && month < 1)
    {
        this->month = 1;
    }
    else
        this->month = month;

    if (day < 1)
        this->day = 1;
    else
    {
        switch (month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (day > 31)
            {
                this->day = 1;
            }
            else
            {
                this->day = day;
            }

            break;
        case 4:
        case 6:
        case 9:
        case 11:
            if (day > 30)
            {
                this->day = 1;
            }
            else
            {
                this->day = day;
            }
            break;
        case 2:
            if (isLeap(year))
            {
                if (day > 29)
                    this->day = 1;
                else
                    this->day = day;
            }
            else
            {
                if (day > 28)
                    this->day = 1;
                else
                    this->day = day;
            }
            break;
        default:
            break;
        }
    }
}
// 计算当前日期与参数日期之间相差几个整年，仅考虑参数日期比当前日期晚的情况
int Date::fullYearsTo(int year, int month, int day)
{
    if (month < this->month)
        year--;
    else if (month == this->month)
    {
        if (day < this->day)
            year--;
    }
    return year - this->year;
}
//计算当前年份已经过的天数
int yearDays(int year, int month, int day)
{
    int sum = 0;
    for (int i = 1; i < month; i++)
    {
        if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10)
            sum += 31;
        else if (i == 2)
        {
            if (isLeap(year))
                sum += 29;
            else
                sum += 28;
        }
        else
            sum += 30;
    }
    sum += day;
    return sum;
}
/* 计算当前日期与参数日期之间相差多少天(考虑闰年)，如果参数日期在当前日期之前，返回负数。 */
int Date::daysTo(int year, int month, int day)
{
    int gapDays = 0;
    if (year >= this->year)
    {
        for (int i = this->year; i < year; i++)
        {
            if (isLeap(i))
                gapDays++;
        }
        gapDays += 365 * (year - this->year);
        gapDays -= yearDays(this->year, this->month, this->day);
        gapDays += yearDays(year, month, day);
    }
    else
    {
        for (int i = year; i < this->year; i++)
        {
            if (isLeap(i))
            {
                gapDays++;
            }
        }
        gapDays += 365 * (this->year - year);
        gapDays += yearDays(this->year, this->month, this->day);
        gapDays -= yearDays(year, month, day);
        gapDays = -gapDays;
    }
    return gapDays;
}